package com.agileboot.attendance.controller;

import com.agileboot.attendance.domain.holiday.db.entity.Holiday;
import com.agileboot.attendance.domain.holiday.dto.HolidayDTO;
import com.agileboot.attendance.domain.holiday.dto.HolidayQueryDTO;
import com.agileboot.attendance.domain.holiday.service.IHolidayService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.core.page.PageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 节假日Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/attendance/holiday")
public class HolidayController {
    
    @Autowired
    private IHolidayService holidayService;
    
    /**
     * 分页查询节假日列表
     */
    @GetMapping("/list")
    public ResponseDTO<PageDTO<HolidayDTO>> list(HolidayQueryDTO queryDTO,
                                 @RequestParam(defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Holiday> page = new Page<>(pageNum, pageSize);
        IPage<HolidayDTO> pageData = holidayService.page(page, queryDTO);
        
        // 转换为统一的PageDTO格式
        PageDTO<HolidayDTO> result = new PageDTO<>();
        result.setRows(pageData.getRecords());
        result.setTotal(pageData.getTotal());
        result.setPageNum(pageData.getCurrent());
        result.setPageSize(pageData.getSize());
        
        return ResponseDTO.ok(result);
    }
    
    /**
     * 获取节假日详情
     */
    @GetMapping("/{holidayId}")
    public ResponseDTO<HolidayDTO> getInfo(@PathVariable Long holidayId) {
        HolidayDTO dto = holidayService.getInfo(holidayId);
        return ResponseDTO.ok(dto);
    }
    
    /**
     * 新增节假日
     */
    @PostMapping
    public ResponseDTO<Boolean> add(@RequestBody HolidayDTO holidayDTO) {
        boolean result = holidayService.add(holidayDTO);
        return ResponseDTO.ok(result);
    }
    
    /**
     * 修改节假日
     */
    @PutMapping
    public ResponseDTO<Boolean> update(@RequestBody HolidayDTO holidayDTO) {
        boolean result = holidayService.update(holidayDTO);
        return ResponseDTO.ok(result);
    }
    
    /**
     * 删除节假日
     */
    @DeleteMapping("/{holidayId}")
    public ResponseDTO<Boolean> delete(@PathVariable Long holidayId) {
        boolean result = holidayService.delete(holidayId);
        return ResponseDTO.ok(result);
    }
    
    /**
     * 批量删除节假日
     */
    @DeleteMapping("/batch/{holidayIds}")
    public ResponseDTO<Boolean> deleteBatch(@PathVariable Long[] holidayIds) {
        boolean result = holidayService.deleteBatch(holidayIds);
        return ResponseDTO.ok(result);
    }
    
    /**
     * 导入指定年份的节假日数据
     */
    @PostMapping("/import/{year}")
    public ResponseDTO<Boolean> importHolidays(@PathVariable Integer year) {
        boolean result = holidayService.importHolidays(year);
        return ResponseDTO.ok(result);
    }
    
    /**
     * 判断指定日期是否为节假日
     */
    @GetMapping("/check")
    public ResponseDTO<Boolean> isHoliday(@RequestParam LocalDate date) {
        boolean result = holidayService.isHoliday(date);
        return ResponseDTO.ok(result);
    }
    
    /**
     * 获取指定年份的所有节假日
     */
    @GetMapping("/year/{year}")
    public ResponseDTO<List<HolidayDTO>> getHolidaysByYear(@PathVariable Integer year) {
        List<HolidayDTO> list = holidayService.getHolidaysByYear(year);
        return ResponseDTO.ok(list);
    }
    
    /**
     * 获取指定月份的所有节假日
     */
    @GetMapping("/month/{year}/{month}")
    public ResponseDTO<List<HolidayDTO>> getHolidaysByMonth(@PathVariable Integer year, @PathVariable Integer month) {
        List<HolidayDTO> list = holidayService.getHolidaysByMonth(year, month);
        return ResponseDTO.ok(list);
    }
} 